<template>
  <div>
    <div class="btn-box">
      <a-button type="primary" icon="plus" @click="addProduct" style="margin-right: 10px">添加产品</a-button>
      <a-alert message="提醒：1、组合产品不能添加为子产品；2、销售状态为开发中、停售的产品不能添加为子产品。3、子产品信息变更时，此处使用的子产品信息会同时变更。" banner />
    </div>
    <a-form-model ref="form" :model="formData">
      <a-table
        bordered
        :pagination="false"
        :columns="formColumns"
        :data-source="formData.tableData"
        :rowKey="(record, index) => index"
      >
        <template #imgSlot="text, record">
          <a-popover trigger="hover" placement="right">
            <template #content>
              <div class="popover-preview-box">
                <img :src="record.murl" />
              </div>
            </template>
            <viewer :images="[record.murl]" class="img-box">
              <img v-show="record.murl" class="sku-img" :src="record.murl" />
              <div class="hint-text">{{ record.murl ? '点击看大图' : '暂无图片' }}</div>
            </viewer>
          </a-popover>
        </template>
        <template #numTitle> <span class="required-ico">*</span>产品数量 </template>
        <template #numSlot="text, record, index">
          <a-form-model-item
            :prop="'tableData.' + index + '.count'"
            :rules="{
              required: true,
              message: '数量不能为空',
              trigger: 'blur',
            }"
          >
            <a-input-number v-model="record.count" :min="1" :step="1" :max="99999999" :precision="0" />
          </a-form-model-item>
        </template>
        <template #sellStatusSlot="text, record">
          <a-tag v-if="record.sellStatus == 2" color="green"> 在售 </a-tag>
          <a-tag v-else-if="record.sellStatus == 1" color="cyan"> 开发中 </a-tag>
          <a-tag v-else-if="record.sellStatus == 3" color="blue"> 清仓 </a-tag>
          <a-tag v-else-if="record.sellStatus == 4" color="red"> 停售 </a-tag>
        </template>
        <span slot="action" slot-scope="text, record, index">
          <a @click="delProduct(index)">删除</a>
        </span>
      </a-table>
    </a-form-model>
    <!-- 产品选择弹窗 -->
    <product-check-modal
      v-if="showProductCheck"
      :defaultParam="{ constituteStatus: 1, deptCode: getBasicForm.$refs.baseForm.formData.deptCode }"
      :visible.sync="showProductCheck"
      :defaultData="formData.tableData"
      @submit="submitProductCheck"
    />
  </div>
</template>

<script>
import ProductCheckModal from '@/components/business/ProductCheckModal.vue'

export default {
  components: { ProductCheckModal },
  props: {
    // 要编辑的产品档案子产品信息
    defaultData: {
      type: Object,
    },
  },
  inject: ['getBasicForm'],
  data() {
    return {
      showProductCheck: false, // 是否显示产品选择弹框
      formData: {
        tableData: [],
      },
      formColumns: [
        {
          title: '序号',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '图片',
          align: 'center',
          scopedSlots: { customRender: 'imgSlot' },
        },
        {
          title: '仓库SKU',
          align: 'center',
          dataIndex: 'skuCode',
        },
        {
          title: '产品名称',
          align: 'left',
          dataIndex: 'productName',
        },
        {
          title: '颜色/尺寸',
          align: 'left',
          dataIndex: 'colorSize',
        },
        {
          align: 'center',
          slots: { title: 'numTitle' },
          scopedSlots: { customRender: 'numSlot' },
        },
        {
          title: '销售状态',
          align: 'center',
          scopedSlots: { customRender: 'sellStatusSlot' },
        },
        {
          title: '操作',
          align: 'center',
          width: 150,
          scopedSlots: { customRender: 'action' },
        },
      ],
    }
  },
  watch: {
    defaultData: {
      handler(val) {
        // 只要有值就赋值给formData
        if (val) {
          const newData = JSON.parse(JSON.stringify(val)) // 深拷贝，避免直接引用导致数据污染
          this.formData.tableData = newData.products
        }
      },
      immediate: true,
    },
  },
  methods: {
    // 添加产品
    addProduct() {
      if (!this.getBasicForm.$refs.baseForm.formData.deptCode) {
        this.$message.error('请填写完【基本信息-成本中心】后再添加子产品。')
        this.getBasicForm.activeKey = 'base'
        return
      }
      //展示产品选择弹框
      this.showProductCheck = true
    },
    //删除产品
    delProduct(index) {
      this.formData.tableData.splice(index, 1)
    },
    submitProductCheck(arr) {
      const newArr = JSON.parse(JSON.stringify(arr))
      this.formData.tableData = newArr.map((i) => {
        let murl = ''
        if (i.murl) {
          murl = i.murl
        } else if (i.hzArchiveInfoImgsList) {
          murl = i.hzArchiveInfoImgsList.find((i2) => i2.izMaster == 1).url
        }
        return {
          colorSize: i.colorSize || i.hzArchiveInfoOtherList[0].color + ';' + i.hzArchiveInfoOtherList[0].size,
          productName: i.productName || i.nameCn,
          sellStatus: i.sellStatus,
          skuCode: i.skuCode,
          count: i.count || 1, // 默认数量为1
          murl,
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.btn-box {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
.popover-preview-box {
  width: 300px;
  height: 300px;
  position: relative;
  background-color: #fafafa;
  img {
    max-height: 100%;
    max-width: 100%;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
}
.img-box {
  width: 60px;
  height: 60px;
  margin-right: 10px;
  display: inline-block;
  cursor: pointer;
  position: relative;
  background-color: #fafafa;
  img {
    width: 100%;
    height: 100%;
  }
  .hint-text {
    width: 100%;
    height: 20px;
    line-height: 20px;
    font-size: 11px;
    text-align: center;
    position: absolute;
    left: 0;
    bottom: 0;
    color: #fff;
    background-color: rgba(0, 0, 0, 0.5);
  }
}
.required-ico {
  margin-right: 4px;
  color: #f5222d;
  font-size: 14px;
}
/deep/ .ant-form-item {
  margin-bottom: 0;
}
</style>
